/* 1) Create an instance of CSInterface. */
var gExtensionID = "com.mid.brushhelper";
var cs = new CSInterface();

/* 2) Make a reference to your HTML button and add a click handler. */
var openButton = document.querySelector("#open-button");
openButton.addEventListener("click", setCurrentBrushSize);

/* 3) Write a helper function to pass instructions to the ExtendScript side. */
function setCurrentBrushSize() {
    cs.evalScript("setCurrentBrushSize(" + brushSize + ")");
}

var bt2 = document.querySelector("#bt2");
bt2.addEventListener("click", function () {
    cs.evalScript("alert('r=>' + getBackgroundColor().rgb.red)");
});


// var curBrushInfo = cs.evalScript("getCurrentBrushSize()");
var brushSize;



function onLoaded() {
    try {
        // Persistent(true);

        themeManager.init();

        var kMinSize = 5;
        var kMaxSize = 100;

        brushSize = kMinSize;
        updateDesc();

        var el = $("#brush-slider");

        // var r = el.slider({
        //     ticks: [7, 20, 150],
        //     ticks_positions: [0, 50, 100],
        //     ticks_snap_bounds: 0,
        //     // formatter: function(value) {
        //     //     return value;
        //     // },
        //     // ticks_tooltip: true,
        //     step: 1
        // });


        var r = el.slider({
            min: kMinSize,
            max: kMaxSize,
            formatter: function(value) {
                return value;
            },
            // ticks_tooltip: true,
            step: 1
        });


        // r.on('slide', function (slideEvt) {
        //     brushSize = slideEvt.value;
        //     setCurrentBrushSize();
        //     updateDesc();
        // });
        r.on('change', function (slideEvt) {
            brushSize = slideEvt.value.newValue;
            setCurrentBrushSize();
            updateDesc();
        });

        // var r = $('input');
        // r.on('mouseenter',function(){
        //     var p = r.val();
        //     r.on('click',function(){
        //         p = r.val();
        //         bg(p);
        //     });
        //     r.on('mousemove',function(){
        //         p = r.val();
        //         bg(p);
        //     });
        // });
        // function bg(n){
        //     r.css({
        //         'background-image':'url("img/arrow.png");'
        //     });
        // }

    } catch (e) {
        alert("onLoaded catch: " + e);
    }
}

function updateDesc() {
    $("#desc").text(brushSize);
}

function Persistent(inOn) {
    gStartDate = new Date();
    var event;
    if (inOn) {
        event = new CSEvent("com.adobe.PhotoshopPersistent", "APPLICATION");
    } else {
        event = new CSEvent("com.adobe.PhotoshopUnPersistent", "APPLICATION");
    }
    event.extensionId = gExtensionID;
    cs.dispatchEvent(event);
}